﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Universum.Infrastructure.Configuration;
using Universum.Infrastructure.Utils.Extensions;
using Universum.Controllers;
using System.Text;
using System.Linq.Expressions;
using StructureMap;
using Universum.Model.Services.Classes;
namespace Universum.Infrastructure.Utils
{
	public class Breadcrumbs
	{
		HtmlHelper _Html;
		public Breadcrumbs(HtmlHelper html)
		{
			_Html = html;
			_Builder = new StringBuilder();
		}
		StringBuilder _Builder=null;
		public Breadcrumbs Home()
		{
			
			_Builder.Append(@"<div id=""breadcrumbs"">");
			_Builder.Append( "&gt;&gt; " + 
			_Html.UserActionLink<HomeController>(c => c.Index(), "Home"));
			return this;
			
			
		}
		public Breadcrumbs Append<TController>(Expression<Action<TController>> action, string text) where TController :Controller
		{
			_Builder.Append("<span> &gt; </span>");
			_Builder.Append(_Html.UserActionLink<TController>(action,text));
			return this;
		}
		public Breadcrumbs AppendText(string text)
		{
			_Builder.Append("<span> &gt; </span>");
			_Builder.Append(string.Format("<span>{0}</span>", text));
			return this;
		}
		public Breadcrumbs Class(Guid classId)
		{
			var @class= ObjectFactory.GetInstance<IClassesService>().GetClassById(classId);
			_Builder.Append("<span> &gt; </span>");
			_Builder.Append(_Html.UserActionLink<ClassesController>(c=>c.EditOrCreateContent(@class.ID), @class.Name??"Unnamed class"));
			return this;

		}
		public Breadcrumbs ClassFromPageGroupId(Guid pageGroupId)
		{
			var pageGroup = ObjectFactory.GetInstance<IPageGroupService>().GetPageGroupById(pageGroupId);
			var @class = ObjectFactory.GetInstance<IClassesService>().GetClassById(pageGroup.ClassId);
			_Builder.Append("<span> &gt; </span>");
			_Builder.Append(_Html.UserActionLink<ClassesController>(c => c.EditOrCreateContent(@class.ID), @class.Name));
			return this;

		}
		public string End(){
			_Builder.Append("</div>");
			return _Builder.ToString();
		}
	}
}
